package com.atstudy.date;

import java.time.*;
import java.util.Date;
import java.util.Set;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2022/12/21 15:06
 * @copyright: 博为峰
 */
public class TimeDemo {
    public static void main(String[] args) {
//        m1();
//        m2();
//        m3();
//        m4();
//        m5();
//        m6();
        //Date -> Instant -> LocalDateTime
        Date date = new Date();
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        //LocalDateTime -> Instant -> Date
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        instant = zonedDateTime.toInstant();
        date = Date.from(instant);
    }

    //间隔（时分秒毫秒纳秒）
    private static void m6() {
        Instant start = Instant.now();
        Instant end = start.plusSeconds(10);
        Duration duration = Duration.between(start, end);
        long seconds = duration.toSeconds();
        System.out.println("seconds = " + seconds);
    }

    //间隔（年月日）
    private static void m5() {
        LocalDate start = LocalDate.now();
        LocalDate end = LocalDate.of(2020, 12, 10);
        Period period = Period.between(end, start);
        int years = period.getYears();
        System.out.println("years = " + years);
        int months = period.getMonths();
        System.out.println("months = " + months);
        int days = period.getDays();
        System.out.println("days = " + days);
    }

    //时区
    private static void m4() {
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        for (String availableZoneId : availableZoneIds) {
//            System.out.println("availableZoneId = " + availableZoneId);
        }
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println("zoneId = " + zoneId);
    }

    //时间加减
    private static void m3() {
        LocalDate localDate = LocalDate.now();
        System.out.println("localDate = " + localDate);
        localDate = localDate.plusYears(10);//加
        localDate = localDate.minusMonths(2);//减
        System.out.println("localDate = " + localDate);
    }

    //设置of()
    private static void m2() {
        LocalDateTime localDateTime = LocalDateTime.of(2019, 11, 10, 12, 15, 30);
        System.out.println("localDateTime = " + localDateTime);
    }

    //当前now()
    private static void m1() {
        Instant instant = Instant.now();//时间戳
        System.out.println("instant = " + instant);

        LocalDate localDate = LocalDate.now();//日期
        System.out.println("localDate = " + localDate);

        LocalDateTime localDateTime = LocalDateTime.now();//日期时间
        System.out.println("localDateTime = " + localDateTime);

        LocalTime localTime = LocalTime.now();//时间
        System.out.println("localTime = " + localTime);

        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println("zonedDateTime = " + zonedDateTime);
    }
}
